package com.csh.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.csh.subject.application.convert.SubjectAnswerDtoConvert;
import com.csh.subject.application.convert.SubjectInfoDtoConvert;
import com.csh.subject.application.dto.SubjectInfoDto;
import com.csh.subject.common.entity.PageResult;
import com.csh.subject.common.entity.Result;
import com.csh.subject.domain.entity.SubjectAnswerBo;
import com.csh.subject.domain.entity.SubjectInfoBo;
import com.csh.subject.domain.service.SubjectInfoDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     *新增题目
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDto.getSubjectName()),"题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(),"题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getCategoryIds()),"分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getLabelIds()),"标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConvert.INSTANCE
                    .convertDtoToBo(subjectInfoDto);
            List<SubjectAnswerBo> subjectAnswerBoList = SubjectAnswerDtoConvert.INSTANCE
                    .convertDtoToBoList(subjectInfoDto.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBoList);
            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     *查询题目列表
     * @return
     */
    @PostMapping ("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(), "标签id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConvert.INSTANCE.convertDtoToBo(subjectInfoDto);
            PageResult<SubjectInfoBo> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        }catch (Exception e){
            log.error("SubjectController.getSubjectPage.error:{}",e.getMessage(),e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     * @param subjectInfoDto
     * @return
     */
    @PostMapping ("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "题目id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConvert.INSTANCE.convertDtoToBo(subjectInfoDto);
            SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto dtoResult = SubjectInfoDtoConvert.INSTANCE.convertBoToDto(boResult);
            return Result.ok(dtoResult);
        }catch (Exception e){
            log.error("SubjectController.querySubjectInfo.error:{}",e.getMessage(),e);
            return Result.fail("查询题目详情失败");
        }
    }
}
